package com.example.anew.journey.mynewjourney.logic.login.model;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.anew.journey.mynewjourney.logic.login.bean.LoginBean;
import com.example.anew.journey.mynewjourney.logic.login.bean.TakeLoginBean;

import java.text.SimpleDateFormat;
import java.util.Date;

import static android.content.Context.MODE_PRIVATE;

/**
 * 杭州惟君医疗科技有限公司10
 * Created by 20433 on 2018/3/23
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class LoginModel implements ISLoginModel {
    //存储数据
    @Override
    public void storeData(LoginBean.DataBean bean, Context mContext) {
        //获取一个文件名为test、权限为private的xml文件的SharedPreferences对象
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("My_App_Cardiac", MODE_PRIVATE);
        //得到SharedPreferences.Editor对象，并保存数据到该对象中
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("my_use_id", bean.id);
        editor.putString("my_use_name", bean.cus_name);
        editor.putString("my_use_tel", bean.tel);
        editor.putString("my_use_logo", bean.cus_logo);
        editor.putString("my_use_birthday", bean.birthday);
        editor.putString("my_use_state", bean.state);
        editor.putString("my_use_qq", bean.qq);
        editor.putString("my_use_weixin", bean.weixin);
        editor.putString("my_use_token", bean.token);
        editor.putBoolean("my_is_login",true);
        editor.putInt("my_stored_data_number",0);
        //保存key-value对到文件中
        editor.commit();

    }


    //取数据
    @Override
    public TakeLoginBean takeData(Context mContext) {
        TakeLoginBean bean = new TakeLoginBean();

        SimpleDateFormat  time = new SimpleDateFormat("yyyy年MM月dd日");
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("My_App_Cardiac", MODE_PRIVATE);
        String my_use_id = sharedPreferences.getString("my_use_id", "");
        String my_use_name = sharedPreferences.getString("my_use_name", "");
        String my_use_tel = sharedPreferences.getString("my_use_tel", "");
        String my_use_logo = sharedPreferences.getString("my_use_logo", "");
        String my_use_birthday = sharedPreferences.getString("my_use_birthday", time.format(new Date()));
        String my_use_state = sharedPreferences.getString("my_use_state", "");
        String my_use_qq = sharedPreferences.getString("my_use_qq", "");
        String my_use_weixin = sharedPreferences.getString("my_use_weixin", "");
        String my_use_token = sharedPreferences.getString("my_use_token", "");

        bean.setId(my_use_id);
        bean.setCus_name(my_use_name);
        bean.setTel(my_use_tel);
        bean.setCus_logo(my_use_logo);
        bean.setBirthday(my_use_birthday);
        bean.setState(my_use_state);
        bean.setQq(my_use_qq);
        bean.setWeixin(my_use_weixin);
        bean.setToken(my_use_token);
        return bean;
    }
}
